package com.artisan.view;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.util.List;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.artisan.dao.ClassDao;
import com.artisan.dao.StudentDao;
import com.artisan.model.Student;
import com.artisan.model.StudentClass;
import com.artisan.model.Teacher;
import com.artisan.util.StringUtil;

import javax.swing.JRadioButton;
import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class ManageStudentFrm extends JInternalFrame {
	private JTextField searchStudentNameTextField;
	private JTable studentListTable;
	private JTextField editStudentNameTextField;
	private JPasswordField editStudentPasswordPasswordField;
	private JComboBox searchStudentComboBox;
	private List<StudentClass> studentClassList;
	private JComboBox editStudentClassComboBox;
	private ButtonGroup editSexButtonGroup;
	private JRadioButton editStudentSexManRadioButton;
	private JRadioButton editStudentSexFemaleRadioButton;
	private JRadioButton editStudentSexUnknowRadioButton;
	private JButton deleteStudentButton;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ManageStudentFrm frame = new ManageStudentFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ManageStudentFrm() {
		setFrameIcon(new ImageIcon(ManageStudentFrm.class.getResource("/com/artisan/image/学生管理.png")));
		setClosable(true);
		setIconifiable(true);

		setTitle("学生信息管理");
		setBounds(100, 100, 911, 586);

		JScrollPane scrollPane = new JScrollPane();

		JLabel lblNewLabel = new JLabel("学生姓名:");
		lblNewLabel.setIcon(new ImageIcon(ManageStudentFrm.class.getResource("/com/artisan/image/学生管理.png")));
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		searchStudentNameTextField = new JTextField();
		searchStudentNameTextField.setColumns(10);

		JLabel lblNewLabel_1 = new JLabel("所属班级:");
		lblNewLabel_1.setIcon(new ImageIcon(ManageStudentFrm.class.getResource("/com/artisan/image/班级名称.png")));
		lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		searchStudentComboBox = new JComboBox();

		JButton searchButton = new JButton("查询");
		searchButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				searchStudent(ae);
			}
		});
		searchButton.setIcon(new ImageIcon(ManageStudentFrm.class.getResource("/com/artisan/image/搜索.png")));
		searchButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		JLabel lblNewLabel_2 = new JLabel("学生姓名:");
		lblNewLabel_2.setIcon(new ImageIcon(ManageStudentFrm.class.getResource("/com/artisan/image/学生管理.png")));
		lblNewLabel_2.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		editStudentNameTextField = new JTextField();
		editStudentNameTextField.setColumns(10);

		JLabel lblNewLabel_3 = new JLabel("所属班级:");
		lblNewLabel_3.setIcon(new ImageIcon(ManageStudentFrm.class.getResource("/com/artisan/image/班级名称.png")));
		lblNewLabel_3.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		editStudentClassComboBox = new JComboBox();

		JLabel lblNewLabel_4 = new JLabel("学生性别:");
		lblNewLabel_4.setIcon(new ImageIcon(ManageStudentFrm.class.getResource("/com/artisan/image/性别.png")));
		lblNewLabel_4.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		editSexButtonGroup = new ButtonGroup();

		editStudentSexManRadioButton = new JRadioButton("男");
		editStudentSexManRadioButton.setSelected(true);
		editStudentSexManRadioButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		editStudentSexFemaleRadioButton = new JRadioButton("女");
		editStudentSexFemaleRadioButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		editStudentSexUnknowRadioButton = new JRadioButton("保密");
		editStudentSexUnknowRadioButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		editSexButtonGroup.add(editStudentSexManRadioButton);
		editSexButtonGroup.add(editStudentSexFemaleRadioButton);
		editSexButtonGroup.add(editStudentSexUnknowRadioButton);

		JLabel lblNewLabel_5 = new JLabel("登陆密码:");
		lblNewLabel_5.setIcon(new ImageIcon(ManageStudentFrm.class.getResource("/com/artisan/image/password.png")));
		lblNewLabel_5.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		editStudentPasswordPasswordField = new JPasswordField();

		JButton submitEditButton = new JButton("确认修改");
		submitEditButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				submitEditAct(ae);
			}
		});
		submitEditButton.setIcon(new ImageIcon(ManageStudentFrm.class.getResource("/com/artisan/image/确认.png")));
		submitEditButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		deleteStudentButton = new JButton("删除学生");
		deleteStudentButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				deleteStudent(ae);
			}
		});
		deleteStudentButton.setIcon(new ImageIcon(ManageStudentFrm.class.getResource("/com/artisan/image/删除.png")));
		deleteStudentButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
				.createSequentialGroup()
				.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
						.addGroup(groupLayout.createSequentialGroup().addGap(112).addGroup(groupLayout
								.createParallelGroup(Alignment.LEADING, false)
								.addGroup(groupLayout.createSequentialGroup().addComponent(lblNewLabel).addGap(18)
										.addComponent(searchStudentNameTextField, GroupLayout.PREFERRED_SIZE, 137,
												GroupLayout.PREFERRED_SIZE)
										.addGap(32).addComponent(lblNewLabel_1).addGap(18)
										.addComponent(searchStudentComboBox, GroupLayout.PREFERRED_SIZE, 167,
												GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addComponent(searchButton, GroupLayout.PREFERRED_SIZE, 97,
												GroupLayout.PREFERRED_SIZE))
								.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 668, GroupLayout.PREFERRED_SIZE)))
						.addGroup(groupLayout.createSequentialGroup().addGap(63)
								.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
										.addComponent(lblNewLabel_3, Alignment.LEADING, GroupLayout.DEFAULT_SIZE,
												GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.addComponent(lblNewLabel_2, Alignment.LEADING, GroupLayout.DEFAULT_SIZE,
												GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
								.addGap(18)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
										.addComponent(editStudentNameTextField)
										.addComponent(editStudentClassComboBox, 0, 166, Short.MAX_VALUE))
								.addGap(39)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
										.addComponent(lblNewLabel_4, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addComponent(lblNewLabel_5))
								.addPreferredGap(ComponentPlacement.RELATED)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
										.addGroup(groupLayout.createSequentialGroup()
												.addComponent(editStudentSexManRadioButton).addGap(18)
												.addComponent(editStudentSexFemaleRadioButton).addGap(14)
												.addComponent(editStudentSexUnknowRadioButton))
										.addComponent(editStudentPasswordPasswordField))
								.addPreferredGap(ComponentPlacement.UNRELATED, 33, Short.MAX_VALUE)
								.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
										.addComponent(deleteStudentButton, GroupLayout.DEFAULT_SIZE,
												GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.addComponent(submitEditButton, GroupLayout.DEFAULT_SIZE,
												GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
				.addContainerGap(119, Short.MAX_VALUE)));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
				.createSequentialGroup().addGap(36)
				.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(searchButton, GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
						.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel)
								.addComponent(searchStudentNameTextField, GroupLayout.PREFERRED_SIZE, 32,
										GroupLayout.PREFERRED_SIZE)
								.addComponent(lblNewLabel_1)
								.addComponent(searchStudentComboBox, GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)))
				.addGap(28).addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 343, GroupLayout.PREFERRED_SIZE)
				.addGap(28)
				.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel_2)
						.addComponent(editStudentNameTextField, GroupLayout.PREFERRED_SIZE, 32,
								GroupLayout.PREFERRED_SIZE)
						.addComponent(editStudentSexManRadioButton).addComponent(editStudentSexUnknowRadioButton)
						.addComponent(editStudentSexFemaleRadioButton).addComponent(submitEditButton)
						.addComponent(lblNewLabel_4))
				.addGap(18)
				.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel_3)
								.addComponent(editStudentClassComboBox, GroupLayout.PREFERRED_SIZE, 31,
										GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(editStudentPasswordPasswordField, GroupLayout.PREFERRED_SIZE, 30,
										GroupLayout.PREFERRED_SIZE)
								.addComponent(lblNewLabel_5))
						.addComponent(deleteStudentButton))
				.addContainerGap()));

		studentListTable = new JTable();
		studentListTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent me) {
				selectedTableRow(me);
			}
		});
		studentListTable.setModel(new DefaultTableModel(new Object[][] {},
				new String[] { "\u5B66\u751F\u7F16\u53F7", "\u5B66\u751F\u59D3\u540D", "\u6240\u5C5E\u73ED\u7EA7",
						"\u5B66\u751F\u6027\u522B", "\u767B\u9646\u5BC6\u7801" }) {
			boolean[] columnEditables = new boolean[] { false, false, false, false, false };

			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		studentListTable.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		scrollPane.setViewportView(studentListTable);
		getContentPane().setLayout(groupLayout);
		setStudentClassInfo();
		setTable(new Student());
		setAuthority();
	}

	protected void submitEditAct(ActionEvent ae) {
		// TODO 自动生成的方法存根
		int row = studentListTable.getSelectedRow();
		if (row == -1) {
			JOptionPane.showMessageDialog(this, "请选中要修改的数据!");
			return;
		}
		String studentName = editStudentNameTextField.getText().toString();
		String studentPassword = String.valueOf(editStudentPasswordPasswordField.getPassword());
		if (StringUtil.isEmpty(studentName)) {
			JOptionPane.showMessageDialog(this, "请填写学生姓名!");
			return;
		}
		if (StringUtil.isEmpty(studentPassword)) {
			JOptionPane.showMessageDialog(this, "请填写密码!");
			return;
		}
		Student student = new Student();
		student.setName(studentName);
		student.setPassword(studentPassword);
		StudentClass sc = (StudentClass) editStudentClassComboBox.getSelectedItem();
		student.setClassId(sc.getId());
		student.setId(Integer.parseInt(studentListTable.getValueAt(row, 0).toString()));
		if (editStudentSexManRadioButton.isSelected()) {
			student.setSex(editStudentSexManRadioButton.getText().toString());
		}
		if (editStudentSexFemaleRadioButton.isSelected()) {
			student.setSex(editStudentSexFemaleRadioButton.getText().toString());
		}
		if (editStudentSexUnknowRadioButton.isSelected()) {
			student.setSex(editStudentSexUnknowRadioButton.getText().toString());
		}
		StudentDao studentDao = new StudentDao();
		if (studentDao.update(student)) {
			JOptionPane.showMessageDialog(this, "更新成功!");
		} else {
			JOptionPane.showMessageDialog(this, "更新失败!");
		}
		studentDao.closeDao();
		setTable(new Student());
	}

	protected void deleteStudent(ActionEvent ae) {
		// TODO 自动生成的方法存根
		int row = studentListTable.getSelectedRow();
		if (row == -1) {
			JOptionPane.showMessageDialog(this, "请选中要删除的数据!");
			return;
		}
		if (JOptionPane.showConfirmDialog(this, "您确定删除吗?", "删除提示", 0) != JOptionPane.OK_OPTION) {
			return;
		}
		StudentDao studentDao = new StudentDao();
		if (studentDao.delete(Integer.parseInt(studentListTable.getValueAt(row, 0).toString()))) {
			JOptionPane.showMessageDialog(this, "删除成功!");
		} else {
			JOptionPane.showMessageDialog(this, "删除失败!");
		}
		studentDao.closeDao();
		setTable(new Student());
	}

	protected void selectedTableRow(MouseEvent me) {
		// TODO 自动生成的方法存根
		DefaultTableModel dft = (DefaultTableModel) studentListTable.getModel();
		editStudentNameTextField.setText(dft.getValueAt(studentListTable.getSelectedRow(), 1).toString());
		editStudentPasswordPasswordField.setText(dft.getValueAt(studentListTable.getSelectedRow(), 4).toString());
		String className = dft.getValueAt(studentListTable.getSelectedRow(), 2).toString();
		for (int i = 0; i < editStudentClassComboBox.getItemCount(); i++) {
			StudentClass sc = (StudentClass) editStudentClassComboBox.getItemAt(i);
			if (className.equals(sc.getName())) {
				editStudentClassComboBox.setSelectedIndex(i);
			}
		}
		String sex = dft.getValueAt(studentListTable.getSelectedRow(), 3).toString();
		editSexButtonGroup.clearSelection();
		if (sex.equals(editStudentSexManRadioButton.getText())) {
			editStudentSexManRadioButton.setSelected(true);
		}
		if (sex.equals(editStudentSexFemaleRadioButton.getText())) {
			editStudentSexFemaleRadioButton.setSelected(true);
		}
		if (sex.equals(editStudentSexUnknowRadioButton.getText())) {
			editStudentSexUnknowRadioButton.setSelected(true);
		}
	}

	protected void searchStudent(ActionEvent ae) {
		// TODO 自动生成的方法存根
		Student student = new Student();
		student.setName(searchStudentNameTextField.getText().toString());
		StudentClass sc = (StudentClass) searchStudentComboBox.getSelectedItem();
		student.setClassId(sc.getId());
		setTable(student);
	}

	private void setTable(Student student) {
		if ("学生".equals(MainFrm.userType.getName())) {
			Student s = (Student) MainFrm.userObject;
			student.setName(s.getName());
		}
		DefaultTableModel dft = (DefaultTableModel) studentListTable.getModel();
		dft.setRowCount(0);
		StudentDao studentDao = new StudentDao();
		List<Student> studentList = studentDao.getStudentList(student);
		for (Student s : studentList) {
			Vector v = new Vector();
			v.add(s.getId());
			v.add(s.getName());
			v.add(getClassNameById(s.getClassId()));
			v.add(s.getSex());
			v.add(s.getPassword());
			dft.addRow(v);
		}
		studentDao.closeDao();
	}

	private void setStudentClassInfo() {
		ClassDao classDao = new ClassDao();
		studentClassList = classDao.getClassList(new StudentClass());
		for (StudentClass sc : studentClassList) {
			searchStudentComboBox.addItem(sc);
			editStudentClassComboBox.addItem(sc);
		}
		classDao.closeDao();
	}

	private String getClassNameById(int id) {
		for (StudentClass sc : studentClassList) {
			if (sc.getId() == id) {
				return sc.getName();
			}
		}
		return "";
	}

	private void setAuthority() {
		if ("学生".equals(MainFrm.userType.getName())) {
			Student s = (Student) MainFrm.userObject;
			searchStudentNameTextField.setText(s.getName());
			searchStudentNameTextField.setEnabled(false);
			deleteStudentButton.setEnabled(false);
			for (int i = 0; i < searchStudentComboBox.getItemCount(); i++) {
				StudentClass sc = (StudentClass) searchStudentComboBox.getItemAt(i);
				if (sc.getId() == s.getClassId()) {
					searchStudentComboBox.setSelectedIndex(i);
					break;
				}
			}
			searchStudentComboBox.setEnabled(false);
			for (int i = 0; i < editStudentClassComboBox.getItemCount(); i++) {
				StudentClass sc = (StudentClass) editStudentClassComboBox.getItemAt(i);
				if (sc.getId() == s.getClassId()) {
					editStudentClassComboBox.setSelectedIndex(i);
					break;
				}
			}
			editStudentClassComboBox.setEnabled(false);
		}
	}
}
